<?php 
/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                     (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it 
    under the terms of the GNU General Public License (version 2) as published 
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with 
    Amrita ITEWS if not then it can be viewed here: 
    http://itews.amrita.ac.in/license.html.
    
    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/
 
    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews"; 
    
    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa 
    Vidyapeetham as part of the project titled,\"Intelligent & Interactive 
    Telematics using Emerging Wireless Technologies for Transport Systems 
    (ITEWS)\" sponsored by Technology Information Forecasting and Assessment 
    Council (TIFAC), India.
***/
?><?php
header('Content-Type: text/html; charset=utf-8');
require_once("./utils.php.inc");
require_once("./settings.php.inc");
require_once("./class.ClientSocket.php");
session_start();

if (!isset($_GET['driverLangCode']) && !isset($_GET['passengerLangCode'])){
	$sql = "SELECT current_driver_lang_code, current_passenger_lang_code FROM persistent_data LIMIT 1";
	if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
		$error_occured = true;
	}
	else {
		if (mysql_num_rows($result)==0){
			$_SESSION['currentDriverLanguage'] = DEFAULT_LANG_FOR_DRIVER_INTERFACE;
			$_SESSION['currentPassengerLanguage'] = DEFAULT_LANG_FOR_PASSENGER_INTERFACE;
		}
		else{
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$_SESSION['currentDriverLanguage'] = $row["current_driver_lang_code"];
				$_SESSION['currentPassengerLanguage'] = $row["current_passenger_lang_code"];
			}
		}
	}
}

if (isset($_GET['driverLangCode'])){
	//$_SESSION['currentContext']="languages";
	//TODO: The error here should be checked by each page, so that we dont lose the context
	log_error("Trying to change the current language to ".$_GET['driverLangCode'],E_USER_NOTICE,__FILE__,__LINE__);
	$sql = "update persistent_data set current_driver_lang_code = '".$_GET['driverLangCode']."'";
	//$sql = "insert into persistent_data (id,current_lang_code,current_trigger_descriptor_id ) values (1,'".$_GET['langCode']."',99999) ON DUPLICATE KEY UPDATE current_lang_code = '".$_GET['langCode']."'";
	if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
		log_error("Query failed - $sql",E_USER_WARNING,__FILE__,__LINE__);
		$_SESSION['flashMessage'] = 'System Error! Please try again later';
		$_SESSION['flashType'] = 'error';
	}
	else if (mysql_affected_rows($link) > 0){
		log_error("Current language changed successfully to ".$_GET['driverLangCode'],E_USER_NOTICE,__FILE__,__LINE__);
		$_SESSION['currentDriverLanguage'] = $_GET['driverLangCode']; // store session data
		$_SESSION['flashMessage'] = 'Driver Language change SUCCESSFUL';
		$_SESSION['flashType'] = 'success';
	}
	else if (mysql_affected_rows($link) == 0){
		log_error("Current language is same as selected language ".$_GET['driverLangCode'],E_USER_NOTICE,__FILE__,__LINE__);
		$_SESSION['currentDriverLanguage'] = $_GET['driverLangCode']; // store session data
		$_SESSION['flashMessage'] = 'No Driver Language change - Same as current language';
		$_SESSION['flashType'] = 'subtle';
	}
	else{
		log_error("Current language change failed",E_USER_WARNING,__FILE__,__LINE__);
		$_SESSION['flashMessage'] = 'Driver Language Change failed! Please try again later';
		$_SESSION['flashType'] = 'fail';
		$_SESSION['flashNumber'] = '500';
	}
}

if (isset($_GET['passengerLangCode'])){
	//$_SESSION['currentContext']="languages";
	//TODO: The error here should be checked by each page, so that we dont lose the context
	log_error("Trying to change the current language to ".$_GET['passengerLangCode'],E_USER_NOTICE,__FILE__,__LINE__);
	$sql = "update persistent_data set current_passenger_lang_code = '".$_GET['passengerLangCode']."'";
	//$sql = "insert into persistent_data (id,current_lang_code,current_trigger_descriptor_id ) values (1,'".$_GET['langCode']."',99999) ON DUPLICATE KEY UPDATE current_lang_code = '".$_GET['langCode']."'";
	if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
		log_error("Query failed - $sql",E_USER_WARNING,__FILE__,__LINE__);
		$_SESSION['flashMessage'] = 'System Error! Please try again later';
		$_SESSION['flashType'] = 'error';
	}
	else if (mysql_affected_rows($link) > 0){
		log_error("Current language changed successfully to ".$_GET['passengerLangCode'],E_USER_NOTICE,__FILE__,__LINE__);
		$_SESSION['currentPassengerLanguage'] = $_GET['passengerLangCode']; // store session data
		$_SESSION['flashMessage'] = 'Passenger Language change SUCCESSFUL';
		$_SESSION['flashType'] = 'success';
	}
	else if (mysql_affected_rows($link) == 0){
		log_error("Current language is same as selected language ".$_GET['passengerLangCode'],E_USER_NOTICE,__FILE__,__LINE__);
		$_SESSION['currentPassengerLanguage'] = $_GET['passengerLangCode']; // store session data
		$_SESSION['flashMessage'] = 'No Passenger Language change - Same as current language';
		$_SESSION['flashType'] = 'subtle';
	}
	else{
		log_error("Current language change failed",E_USER_WARNING,__FILE__,__LINE__);
		$_SESSION['flashMessage'] = 'Passenger Language Change failed! Please try again later';
		$_SESSION['flashType'] = 'fail';
		$_SESSION['flashNumber'] = '500';
	}
}

if (isset($_GET['routeCode']) && !AUTO_PILOT){
	//$_SESSION['currentContext']="changeRoute";
	//TODO: The error here should be checked by each page, so that we dont lose the context
	log_error("Trying to change the current route to ".$_GET['routeCode'],E_USER_NOTICE,__FILE__,__LINE__);
	$osgi_response_successful = 0;
	try {
		$sc = new ClientSocket();
		$sc->open("localhost",OSGI_TCP_PORT);
		$sc->send((strcmp($_GET['routeCode'],"---")==0)?"noroute\n":$_GET['routeCode']."\n");
		$osgi_response_successful = intval($sc->recv(OSGI_CONNECTION_TIMEOUT));
	}
	catch (Exception $e){
		log_error($e->getMessage(),E_USER_WARNING,__FILE__,__LINE__);
		$osgi_response_successful = 0;
	}

	//check for any problems and the return code, to make sure that the entry was made into database
	if($osgi_response_successful){
		log_error("Current route changed successfully to ".$_GET['routeCode'],E_USER_NOTICE,__FILE__,__LINE__);
		$_SESSION['currentRoute'] = $_GET['routeCode']; // store session data
		$_SESSION['flashMessage'] = 'Route Change successful!';
		$_SESSION['flashType'] = 'success';
		$_SESSION['flashNumber'] = '219';
	}
	else {
		log_error("Current Route change failed",E_USER_WARNING,__FILE__,__LINE__);
		$_SESSION['flashMessage'] = 'Route Change failed. Please try again later';
		$_SESSION['flashType'] = 'fail';
		$_SESSION['flashNumber'] = '220';
	}
}

//TODO: save current context also in DB
//TODO: PROCESSING ICON SHOULD COME
//TODO: THE FLASH MESSAGES HAVE TO APPEAR


if (isset($_SESSION['currentContext']))
	$currCtx = $_SESSION['currentContext'];
else
	$currCtx = "home";
	
switch ($currCtx){
	case "home":
		$centerPage = "homescreen.php";
		break;
	case "settings":
		$centerPage = "settingsscreen.php";
		break;
	case "changeRoute":
		$centerPage = "changeroutescreen.php";
		break;
	case "downloadStatus":
		$centerPage = "downloadstatusscreen.php";
		break;
	case "driverLanguages":
		$centerPage = "driverlanguagesscreen.php";
		break;
	case "passengerLanguages":
		$centerPage = "passengerlanguagesscreen.php";
		break;
}

?>
<frameset rows="22%,68%,11%" border="0">
	<frame name="topFrame" src="./header.php"/>
	<!--frame name="messageFrame" src=''/-->
	<frame name="centerFrame" src="<?php echo $centerPage ?>"/><!--load the current context screen if any otherwise the home screen or splash screen-->
	<!--frame name="bottomFrame" src="webticker.php"/-->
	<frame name="bottomFrame" src="./station_info.php"/>
</frameset>
